package com.example.shopdemo.controller.dashboard;

import com.example.shopdemo.dto.ProductExecution;
import com.example.shopdemo.entity.Product;
import com.example.shopdemo.entity.ProductCategory;
import com.example.shopdemo.entity.Shop;
import com.example.shopdemo.service.ProductCategoryServiceImpl;
import com.example.shopdemo.service.ProductServiceImpl;
import com.example.shopdemo.service.ShopServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-27
 **/
@Controller
@RequestMapping("/dashboard")
public class ShopDetailController {

    @Autowired
    private ShopServiceImpl shopService;

    @Autowired
    private ProductCategoryServiceImpl productCategoryService;

    @Autowired
    private ProductServiceImpl productService;

    @RequestMapping("/listShopDetailPageInfo")
    @ResponseBody
    public Map<String,Object> listShopDetailPageInfo(@RequestParam("shopId")Long shopId){
        Map<String,Object> modelMap = new HashMap<>();
        Shop shop = shopService.getShopByShopId(shopId);
        List<ProductCategory>  productCategoryList = productCategoryService.getByShopId(shopId);
        modelMap.put("shop",shop);
        modelMap.put("productCategoryList",productCategoryList);
        modelMap.put("success",true);
        return modelMap;
    }


    @RequestMapping("/listProductsByShop")
    @ResponseBody
    public Map<String,Object> listProductsByShop(@RequestParam("page")int page,@RequestParam("size")int size,
                                                 @RequestParam(value = "shopId",required = false)Long shopId,@RequestParam(value = "productCategoryId",required = false)Long productCategoryId,
                                                 @RequestParam(value = "productName",required =true )String productName){
        Map<String,Object> modelMap = new HashMap<>();
        Product productCondition = new Product();
        if(shopId!=null){
            productCondition.setShopId(shopId);
        }
        if(productCategoryId!=null){
            productCondition.setProductCategoryId(productCategoryId);
        }
        if(productName!=null){
            productCondition.setProductName(productName);
        }
        productCondition.setEnableStatus(1);
        ProductExecution productExecution = productService.getProductList(productCondition,page,size);
        if(productExecution.getProductList()!=null){
            modelMap.put("productList",productExecution.getProductList());
            modelMap.put("count",productExecution.getCount());
            modelMap.put("success",true);
        }else{
            modelMap.put("success",false);
            modelMap.put("errMsg",productExecution.getStateInfo());
        }
        return modelMap;

    }
}
